#ifndef UART_INT_H_
#define UART_INT_H_

//Habilita as uart
#define ___UART0	0
#define ___UART1	0
#define ___UART2	1
#define ___UART3	0

/* buffer size definition */
#define UART_RING_BUFSIZE 256

/* Buf mask */
#define __BUF_MASK (UART_RING_BUFSIZE-1)
/* Check buf is full or not */
#define __BUF_IS_FULL(head, tail) ((tail&__BUF_MASK)==((head+1)&__BUF_MASK))
/* Check buf will be full in next receiving or not */
#define __BUF_WILL_FULL(head, tail) ((tail&__BUF_MASK)==((head+2)&__BUF_MASK))
/* Check buf is empty */
#define __BUF_IS_EMPTY(head, tail) ((head&__BUF_MASK)==(tail&__BUF_MASK))
/* Reset buf */
#define __BUF_RESET(bufidx)	(bufidx=0)
#define __BUF_INCR(bufidx)	(bufidx=(bufidx+1)&__BUF_MASK)

/** @brief UART Ring buffer structure */
typedef struct
{
    __IO uint32_t tx_head;                /*!< UART Tx ring buffer head index */
    __IO uint32_t tx_tail;                /*!< UART Tx ring buffer tail index */
    __IO uint32_t rx_head;                /*!< UART Rx ring buffer head index */
    __IO uint32_t rx_tail;                /*!< UART Rx ring buffer tail index */
    __IO uint8_t  tx[UART_RING_BUFSIZE];  /*!< UART Tx data ring buffer */
    __IO uint8_t  rx[UART_RING_BUFSIZE];  /*!< UART Rx data ring buffer */
    __IO FlagStatus TxIntStat;
} UART_RING_BUFFER_T;
#if ___UART0
extern UART_RING_BUFFER_T buffer_uart0;
#endif
#if ___UART1
extern UART_RING_BUFFER_T buffer_uart1;
#endif
#if ___UART2
extern UART_RING_BUFFER_T buffer_uart2;
#endif
#if ___UART3
extern UART_RING_BUFFER_T buffer_uart3;
#endif
/* uart0 */
#if ___UART0
void UART0_IRQHandler(void);
void UART0_IntErr(uint8_t bLSErrType);
void UART0_IntTransmit(void);
void UART0_IntReceive(void);

uint32_t UART0Receive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen);
uint32_t UART0Send(LPC_UART_TypeDef *UARTPort, uint8_t *txbuf, uint8_t buflen);
void uart0_init(void);
#endif

/*Uart1*/
#if ___UART1
void UART1_IRQHandler(void);
void UART1_IntTransmit(void);
void UART1_IntReceive(void);
void UART1_IntErr(uint8_t bLSErrType);

uint32_t UART1Receive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen);
uint32_t UART1Send(LPC_UART_TypeDef *UARTPort, uint8_t *txbuf, uint8_t buflen);
void uart1_init(void);
#endif

/*Uart2*/
#if ___UART2
void UART2_IRQHandler(void);
void UART2_IntTransmit(void);
void UART2_IntReceive(void);
void UART2_IntErr(uint8_t bLSErrType);

uint32_t UART2Receive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen);
uint32_t UART2Send(LPC_UART_TypeDef *UARTPort, uint8_t *txbuf, uint8_t buflen);
void uart2_init(void);
#endif

/*Uart3*/
#if ___UART3
void UART3_IRQHandler(void);
void UART3_IntTransmit(void);
void UART3_IntReceive(void);
void UART3_IntErr(uint8_t bLSErrType);

uint32_t UART3Receive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen);
uint32_t UART3Send(LPC_UART_TypeDef *UARTPort, uint8_t *txbuf, uint8_t buflen);
void uart3_init(void);
#endif

#if ((___UART0|___UART1|___UART2|___UART3)==0)
#error "Nenhuma porta serial foi definida. Verificar se esta sendo necessaria a utilizacao da mesma no projeto"
#endif

#endif
